package com.tj.collection.integration.impl;

import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.EntityUtils;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApplicationException;
import com.qianli.cashmama.product.facade.AppFacade;
import com.qianli.cashmama.product.facade.BusinessProductFacade;
import com.qianli.cashmama.product.facade.ProductFacade;
import com.qianli.cashmama.product.facade.enums.PagePositionEnum;
import com.qianli.cashmama.product.facade.request.AppBusinessProductRequest;
import com.qianli.cashmama.product.facade.result.*;
import com.tj.collection.integration.ProductIntegrationService;
import com.tj.collection.integration.dto.AppBusinessProductDTO;
import com.tj.collection.integration.dto.BusinessProductDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiatian on 2017/11/3.
 */
@Service
public class ProductIntegrationServiceImpl implements ProductIntegrationService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductIntegrationServiceImpl.class);

    @Autowired
    private ProductFacade productFacade;
    @Autowired
    private BusinessProductFacade businessProductFacade;
    @Autowired
    private AppFacade appFacade;

    @Override
    public Response<List<ProductCategory>> getProductCategoryList(String appCode) {
        return productFacade.getProductCategoryList(appCode);
    }

    @Override
    public Response<Product> getProductDetail(String productCode, boolean selectRules) {
        return productFacade.getProductDetail(productCode, selectRules);
    }


    @Override
    public Response<List<ProductCategory>> getAllActiveProductCategory() {
        return productFacade.getAllActiveProductCategory();
    }

    @Override
    public Response<List<ProductCategory>> getAllActiveProductCategoryByMerchantCode(String merchantCode) {
        return productFacade.getProductCategoriesByMerchantCode(merchantCode);
    }

    @Override
    public Response<List<Product>> getProductList(String productCategory) {
        Response<List<Product>> response = productFacade.getProductList(productCategory, false);
        return response;
    }

    @Override
    public ProductCategory getByProductCategory(String productCategory) {
        if (StringUtils.isEmpty(productCategory)) {
            return null;
        }
        Response<ProductCategory> categoryByCode = productFacade.getCategoryByCode(productCategory);
        if (categoryByCode == null) {
            return null;
        }

        return categoryByCode.getData();
    }

    /**
     * 获取产品信息
     *
     * @param productCodeList
     * @return
     */
    @Override
    public List<Product> getProductList(List<String> productCodeList) {
        List<Product> productList = new ArrayList<>();
        for (String productCode : productCodeList) {
            Response<Product> productResponse = productFacade.getProductDetail(productCode, false);
            if (!productResponse.isSuccess()) {
                return null;
            }
            if (productResponse.getData() == null) {
                continue;
            }
            productList.add(productResponse.getData());
        }
        return productList;
    }

    /**
     * 获取产品信息
     *
     * @param productCodeList
     * @return
     */
    @Override
    public Map<String, Product> getProductMap(List<String> productCodeList) {
        List<Product> productList = this.getProductList(productCodeList);
        if (CollectionUtils.isEmpty(productList)) {
            return new HashMap<>();
        }
        return EntityUtils.makeEntityMap(productList, "code");
    }

    @Override
    public List<BusinessProduct> getBusinessProductList() {
        Response<List<BusinessProduct>> response = businessProductFacade.getBusinessProductList();
        if (!response.isSuccess()) {
            throw new ApplicationException(response.getMsg());
        }
        return response.getData();
    }

    @Override
    public List<AppBusinessProduct> getAppBusinessProductList(Integer position) {
        PagePositionEnum pagePositionEnum = PagePositionEnum.getPagePositionEnumByPosition(position);
        Response<List<AppBusinessProduct>> appBusinessProductList = businessProductFacade.getAppBusinessProductList(pagePositionEnum);
        if (!appBusinessProductList.isSuccess()) {
            throw new ApplicationException(appBusinessProductList.getMsg());
        }
        return appBusinessProductList.getData();
    }

    @Override
    public List<AppBusinessProduct> getAppBusinessProductList(Integer position, Long businessProductId) {
        Response<List<AppBusinessProduct>> appBusinessProductList = businessProductFacade.getAppBusinessProductList(PagePositionEnum.getPagePositionEnumByPosition(position), businessProductId);
        if (!appBusinessProductList.isSuccess()) {
            throw new ApplicationException(appBusinessProductList.getMsg());
        }
        return appBusinessProductList.getData();
    }

    @Override
    public List<AppBusinessProduct> getAppBusinessProductList(AppBusinessProductDTO appBusinessProductDTO) {
        AppBusinessProductRequest request = new AppBusinessProductRequest();
        BeanUtils.copyProperties(appBusinessProductDTO, request);
        Response<List<AppBusinessProduct>> appBusinessProductList = businessProductFacade.getAppBusinessProductList(request);
        return appBusinessProductList.getData();
    }

    @Override
    public AppBusinessProducts getAppBusinessProductListByPage(Map<String, Object> paramMap, Page page) {
        PagePositionEnum pagePositionEnum = null;
        if (paramMap.get("position") != null) {
            Integer position = (Integer) paramMap.get("position");
            pagePositionEnum = PagePositionEnum.getPagePositionEnumByPosition(position);
        }
        Response<AppBusinessProducts> appBusinessProducts = businessProductFacade.getAppBusinessProductListByPage(pagePositionEnum, page);
        return appBusinessProducts.getData();
    }

    @Override
    public boolean addAppBusinessProduct(AppBusinessProductDTO appBusinessProductDTO) {
        AppBusinessProduct appBusinessProduct = new AppBusinessProduct();
        BeanUtils.copyProperties(appBusinessProductDTO, appBusinessProduct);
        Response<Boolean> response = businessProductFacade.addAppBusinessProduct(appBusinessProduct);
        LOGGER.info("addAppBusinessProduct response:{}", JSONObject.toJSONString(response));
        if (!response.isSuccess()) {
            throw new ApplicationException(response.getMsg());
        }
        return response.getData();
    }

    @Override
    public boolean addBusinessProduct(BusinessProductDTO businessProductDTO) {
        BusinessProduct businessProduct = new BusinessProduct();
        BeanUtils.copyProperties(businessProductDTO, businessProduct);
        Response<Boolean> response = businessProductFacade.addBusinessProduct(businessProduct);
        if (!response.isSuccess()) {
            throw new ApplicationException(response.getMsg());
        }
        return response.getData();
    }

    @Override
    public boolean deleteBusinessProduct(Long id) {
        Response<Boolean> response = businessProductFacade.deleteBusinessProduct(id);
        if (!response.isSuccess()) {
            throw new ApplicationException(response.getMsg());
        }
        return response.getData();
    }

    @Override
    public List<AppProductCategoryVo> getAllAppProductCategory() {
        try {
            List<AppProductCategoryVo> list = appFacade.getAllAppProductCategory().getData();
            return list;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<AppVo> getAllApps() {
        try {
            List<AppVo> list = appFacade.getAllApps().getData();
            return list;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public AppPackageInfo getAppPackageInfoByPackageName(String packageName) {
        try {
            return appFacade.getAppPackageInfoByPackageName(packageName).getData();
        } catch (Exception e) {
            return null;
        }
    }
}
